/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lambkit.module.upms.server.service.impl;

import com.jfinal.plugin.activerecord.Db;
import com.lambkit.Lambkit;
import com.lambkit.common.service.LambkitModelServiceImpl;
import com.lambkit.common.service.LambkitService;
import com.lambkit.common.util.DateTimeUtils;
import com.lambkit.db.sql.column.Example;
import com.lambkit.module.upms.model.UpmsCreditModel;
import com.lambkit.module.upms.model.UpmsUserCreditModel;
import com.lambkit.module.upms.server.service.*;
import com.lambkit.common.service.Service;

import java.util.Date;

/**
 * @author yangyong 
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2022-01-19
 * @version 1.0
 * @since 1.0
 */
@Service
@LambkitService
public class UpmsCreditModelServiceImpl extends LambkitModelServiceImpl<UpmsCreditModel> implements UpmsCreditModelService {
	
	private UpmsCreditModel DAO = null;
	
	public UpmsCreditModel dao() {
		if(DAO==null) {
			DAO = Lambkit.singleton(UpmsCreditModel.class);
		}
		return DAO;
	}
	
	/***
	 *  每日登录得分
	 */
	private static final int LOGIN_SCORE = 2;

	/***
	 *  缓存名称
	 */
	private static final String CACHE_NAME_DAY_CREDIT = "CACHE_NAME_DAY_CREDIT";

	@Override
	public void addDayLogin(Long userId, String action) {
		// TODO Auto-generated method stub
		String todayStr = DateTimeUtils.format(new Date(), "yyyy-MM-dd");
		if (Lambkit.getCache().get(CACHE_NAME_DAY_CREDIT, todayStr + userId) != null) {// 当天是否有领取
			return;
		}
		
		UpmsCreditModel upmsCredit = new UpmsCreditModel();
		upmsCredit.setUserId(userId);
		upmsCredit.setCredit(LOGIN_SCORE);
		upmsCredit.setCtime(new Date());
		upmsCredit.setCtype("login");
		upmsCredit.setAction(action);
		
		Db.tx(()-> {
			boolean flag = upmsCredit.save();
			if(!flag) return false;
			
			Example example = UpmsUserCreditModel.sql().andUserIdEqualTo(userId).example();
			UpmsUserCreditModel upmsUserCredit = UpmsUserCreditModel.service().findFirst(example);
			if(upmsUserCredit == null){
				upmsUserCredit = new UpmsUserCreditModel();
				upmsUserCredit.setUserId(userId);
				upmsUserCredit.setCredit(LOGIN_SCORE);
				return upmsUserCredit.save();
			}else {
				upmsUserCredit.setCredit(upmsUserCredit.getCredit() + LOGIN_SCORE);
				return upmsUserCredit.update();
			}
		});
		
		// 缓存领取信息
		Lambkit.getCache().put(CACHE_NAME_DAY_CREDIT, todayStr + userId, "已领取", 60 * 60 * 24);
	}

	@Override
	public void addCredit(Long userId, int credit, String type, String action) {
		// TODO Auto-generated method stub
		UpmsCreditModel upmsCredit = new UpmsCreditModel();
		upmsCredit.setUserId(userId);
		upmsCredit.setCredit(credit);
		upmsCredit.setCtime(new Date());
		upmsCredit.setCtype(type);
		upmsCredit.setAction(action);
		
		Db.tx(()-> {
			boolean flag = upmsCredit.save();
			if(!flag) return false;
			
			Example example = UpmsUserCreditModel.sql().andUserIdEqualTo(userId).example();
			UpmsUserCreditModel upmsUserCredit = UpmsUserCreditModel.service().findFirst(example);
			if(upmsUserCredit == null){
				upmsUserCredit = new UpmsUserCreditModel();
				upmsUserCredit.setUserId(userId);
				upmsUserCredit.setCredit(credit);
				return upmsUserCredit.save();
			}else {
				upmsUserCredit.setCredit(upmsUserCredit.getCredit() + credit);
				return upmsUserCredit.update();
			}
		});
	}
}
